﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Xml;

using FetchAndProcess.Models;
using log4net;

namespace WebDUPlugIn.ConfigAndModels
{

    public class UrlData
    {
        private static readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
        public UrlData() { }
        public UrlData(string name , string uri )
        {
            this.Name = name;
            this.Uri = new Uri(uri);
        }

        public UrlData(UrlData url)
        {
            this.Name = url.Name;
            this.Uri = url.Uri;
        }

        public string Name { get; set; }
        public Uri Uri { get; set; }
    }

    public class WebDUConfig: FetcherConfigBaseRec
    {
        private static readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

        private const string urlNodeStr = "URL";
        private const string nameStr = "name";
        private const string uriStr = "URI";

        private const string nameAttrString = "name";
        private const string typeAttrStr = "type";
        private const string activeAttrString = "active";


        private List<UrlData> UrlNodeList { get; set; }


        private bool  LoadBasicConfiguration( XmlNode fetcherNode)
        {
            XmlAttribute xmlAttr = null;

            if( fetcherNode == null )
            {
                return false;
            }
            // read fetcher Name Attribute 
            xmlAttr = fetcherNode.Attributes[nameAttrString];
            if (xmlAttr == null)
            {
                log.DebugFormat("AttributeMissing : " + nameAttrString + " :  For fetcher Record ");
                return false;
            }
            _FetcherName = xmlAttr.Value;

            log.DebugFormat("Name: " + FetcherName);

            // read fetcher Type  Attribute 
            xmlAttr = fetcherNode.Attributes[typeAttrStr];
            if (xmlAttr == null)
            {
                log.FatalFormat("AttributeMissing : " + typeAttrStr + " :  For fetcher Record ");
                return false;
            }

            _FetcherType = xmlAttr.Value;

            // Read Active String 
            _isActive = true;
            xmlAttr = fetcherNode.Attributes[activeAttrString];
            if (xmlAttr != null)
            {
                _isActive = System.Convert.ToBoolean(xmlAttr.Value);
            }

            return true;
        }

        /// <summary>
        /// Get Read Only List , we ond't want user to change this data 
        /// </summary>
        public ReadOnlyCollection<UrlData> UrlParentNodes
        {
            get
            {
                var readOnlyCollection = new ReadOnlyCollection<UrlData>(UrlNodeList);
                return readOnlyCollection;
            }

        }

        public WebDUConfig()
        {
            UrlNodeList = new List<UrlData>();
        }

        public bool LoadConfiguration(XmlNode fetcherNode)
        {
            // Name , Type ,Active 

            if( !LoadBasicConfiguration(fetcherNode) )
            {
                log.WarnFormat("Basic Configuration Items Missing..., chk name /type/ active ");
                return false;
            }


            // get PolyGon Co ordinates
            XmlNodeList urlNodes = fetcherNode.SelectNodes(urlNodeStr);
            if (urlNodes == null)
            {
                log.DebugFormat("URL Nodes Missing , Void Config ");
                return false;
            }

            log.DebugFormat("Reading URL nodes data .............  ");

            foreach (XmlNode urlNode in urlNodes)
            {
                XmlAttribute xmlAttr = null;
                var url = new UrlData();
                if (urlNode.Attributes == null)
                {
                    log.WarnFormat("No Attributes  for one row in this Configuration.. " + FetcherName);
                }
                xmlAttr = urlNode.Attributes[nameStr];
                if (xmlAttr == null)
                {
                    return false;
                }
                url.Name = xmlAttr.Value;

                xmlAttr = urlNode.Attributes[uriStr];
                if (xmlAttr == null)
                {
                    return false;
                }
                url.Uri = new Uri(xmlAttr.Value);
                // Add to List
                UrlNodeList.Add(url);
            }

            log.DebugFormat("Finsihed Reading Fetcher Record for " + FetcherName);
            return true;
        }

    }

}
